<?php defined('BASEPATH') OR die('No direct access allowed.');
/**
 * Description of Ajax
 *
 * Created on Dec 20, 2010
 * @author Ramon Gutierrez
 * @copyright Bitstop Network Services Inc.
 * @version 0.1
 */
class Ajax extends Controller {
	function Ajax() {
		parent::Controller();
		
		// Prevent direct access to this page
		if(!IS_AJAX) {
			show_404('page');
		}
		
		// Load Flexigrid library
		$this->load->library('flexigrid');

		// Debug
		//$this->output->enable_profiler(TRUE);
		
		// Load common model
		$this->load->model('ajax_model');
	}
	
	function index() {
		
	}
	
	/*
	##  branches_getrows()
	##  -------------------------
	##  Generates branch management table
	*/#####################################
	function branches_getrows($client_id = false) {
		// A list of all valid fields
		$valid_fields = array('b.activated', 'b.name', 'name', 'branch_manager', 'b.product_key');
		
		$this->flexigrid->validate_post('b.name', 'asc', $valid_fields);

		// Let's grab those rows!
		$records = $this->ajax_model->get_branches($client_id);

		// Build with json_encode :D
		if($records['records']->num_rows() > 0) {	
			foreach($records['records']->result() as $row) {
				$actions = //'<a href="' . site_url('branches/info/' . $row->id) . '/"><img src="' . site_url('images/btn-info.png') . '" alt="Info" title="More information" style="margin: 0 5px 0 5px;"></a>' .
						   '<a href="' . site_url('branches/edit/' . $row->branch_id) . '/"><img src="' . site_url('images/btn-edit.png') . '" alt="Edit" title="Edit this item" style="margin: 0 5px 0 5px;"></a>' .
						   '<img id="' . $row->branch_id . '|del|' . $row->name . '" onclick="delete_item(this.id);" src="' . site_url('images/btn-delete.png') . '" alt="Delete" style="margin: 0 5px 0 5px; cursor: pointer;" title="Delete this item">';
				
				$active = $row->activated ? 'Activated' : 'Inactive';
				
				if($this->session->userdata('account_type') == 'site_admin') {
					$record_items[] = array($row->branch_id,
											'<img src="' . site_url("images/icon-" . strtolower($active) . ".png") . '" alt="' . $active . '" title="' . $active . '" />',
											//'<a href="' . site_url('clients/edit/' . $row->client_id) . '" title="More information">' . $row->company . '</a>',
											'<b>' . $row->company . '</b>',
											'<b>' . $row->name . '</b>',
											'<a href="' . site_url('accounts/edit/' . $row->account_id) . '" title="More information">' . $row->branch_manager . '</a>',
											$row->product_key,
											$actions
										   );
				} else{
					$record_items[] = array($row->branch_id,
											'<img src="' . site_url("images/icon-" . strtolower($active) . ".png") . '" alt="' . $active . '" title="' . $active . '" />',
											'<b>' . $row->name . '</b>',
											'<a href="' . site_url('accounts/edit/' . $row->account_id) . '" title="More information">' . $row->branch_manager . '</a>',
											$row->product_key,
											$actions
										   );
				}
			}
		}
		else
		{
			$record_items = array();
		}
			
		//Print please
		$this->output->set_header($this->config->item('json_header'));
		$this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
	}
	
	/*
	##  services_getrows()
	##  -------------------------
	##  Generates branch management table
	*/#####################################
	function services_getrows($client_id = false) {
		// A list of all valid fields
		$valid_fields = array('id', 'title', 'prefix', 'access', 'description', 'c.name');
		
		$this->flexigrid->validate_post('title', 'asc', $valid_fields);

		// Let's grab those rows!
		$records = $this->ajax_model->get_services($client_id);
		
		// Load local model
		$this->load->model('services_model');

		// Build with json_encode :D
		if($records['records']->num_rows() > 0) {	
			foreach($records['records']->result() as $row) {
				$actions = //'<a href="' . site_url('services/info/' . $row->id) . '/"><img src="' . site_url('images/btn-info.png') . '" alt="Info" title="More information" style="margin: 0 5px 0 5px;"></a>' .
						   '<a href="' . site_url('services/edit/' . $row->id) . '/"><img src="' . site_url('images/btn-edit.png') . '" alt="Edit" title="Edit this item" style="margin: 0 5px 0 5px;"></a>' .
						   ($this->session->userdata('account_type') == 'company' || $row->access ? '<img id="' . $row->id . '|del|' . $row->title . '" onclick="delete_item(this.id);" src="' . site_url('images/btn-delete.png') . '" alt="Delete" style="margin: 0 5px 0 5px; cursor: pointer;" title="Delete this item">' : '');
				
				// Count global users
				if($row->access) {
					$users = $this->services_model->globalUsers($row->id);
				}
				
				if($this->session->userdata('account_type') == 'site_admin') {
					$record_items[] = array($row->id,
											$row->id,
											'<b>' . $row->title . '</b>',
											$row->prefix,
											$row->access == 1 ? 'Global (<b>' . ($users ? $users : 'No') . '</b> user' . ($users == 1 ? '' : 's') . ')' : '<a href="' . site_url('clients/info/' . $row->client_id) . '" title="More information">' . $row->name . '</a>',
											$row->access == 1 ? '<img src="' . site_url('images/icon-yes.png') . '" alt="Yes" title="Yes" />' : '<img src="' . site_url('images/icon-no.png') . '" alt="No" title="No" />',
											$actions
										   );
				} else{
					$record_items[] = array($row->id,
											$row->id,
											'<b>' . $row->title . '</b>',
											$row->prefix,
											$row->description,
											$actions
										   );
				}
			}
		}
		else
		{
			$record_items = array();
		}
			
		//Print please
		$this->output->set_header($this->config->item('json_header'));
		$this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
	}
	
	/*
	##  accounts_getrows()
	##  -------------------------
	##  Generates accounts management table
	*/######################################
	function accounts_getrows($client_id = false) {
		// A list of all valid fields
		$valid_fields = array('account_id', 'last_name', 'first_name', 'username', 'account_type', 'name');
		
		$this->flexigrid->validate_post('last_name', 'asc', $valid_fields);

		// Let's grab those rows!
		$records = $this->ajax_model->get_accounts($client_id);

		// Build with json_encode :D
		if($records['records']->num_rows() > 0) {	
			foreach($records['records']->result() as $row) {
				$actions = //'<a href="' . site_url('accounts/info/' . $row->id) . '/"><img src="' . site_url('images/btn-info.png') . '" alt="Info" title="More information" style="margin: 0 5px 0 5px;"></a>' .
						   '<a href="' . site_url('accounts/edit/' . $row->account_id) . '/"><img src="' . site_url('images/btn-edit.png') . '" alt="Edit" title="Edit this item" style="margin: 0 5px 0 5px;"></a>' .
						   ($row->account_type == 'agent' ? '<img id="' . $row->account_id . '|del|' . $row->username . '" onclick="delete_item(this.id);" src="' . site_url('images/btn-delete.png') . '" alt="Delete" style="margin: 0 5px 0 5px; cursor: pointer;" title="Delete this item">' : '');
				
				if($this->session->userdata('account_type') == 'site_admin') {
					$record_items[] = array($row->account_id,
											$row->account_id,
											$row->last_name,
											$row->first_name,
											'<b>' . $row->username . '</b>',
											'<b>' . ucfirst($row->account_type) . '</b>',
											'<a href="' . site_url('clients/info/' . $row->client_id) . '" title="More information">' . $row->company . '</a>',
											$actions
										   );
				} else{
					$record_items[] = array($row->account_id,
											$row->account_id,
											$row->last_name,
											$row->first_name,
											'<b>' . $row->username . '</b>',
											'<b>' . ucfirst($row->account_type) . '</b>',
											$actions
										   );
				}
			}
		}
		else
		{
			$record_items = array();
		}
		
		//Print please
		$this->output->set_header($this->config->item('json_header'));
		$this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
	}
}

/* End of file ajax.php */
/* Location: ./system/application/controllers/ajax.php */